package com.zhongxi.guoyuan.view.personalCenter;

import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.google.gson.Gson;
import com.handmark.pulltorefresh.library.PullToRefreshListView;
import com.zhongxi.guoyuan.R;
import com.zhongxi.guoyuan.adapter.rewardCoin.RewardDoorAdapter;
import com.zhongxi.guoyuan.adapter.SliverOrderAdapter;
import com.zhongxi.guoyuan.adapter.personCenter.InnerOrderAdapter;
import com.zhongxi.guoyuan.application.MyApplication;
import com.zhongxi.guoyuan.base.BaseActivity;
import com.zhongxi.guoyuan.entity.BaseResponse;
import com.zhongxi.guoyuan.entity.center.InnerOrderResult;
import com.zhongxi.guoyuan.entity.center.RewardDoorResult;
import com.zhongxi.guoyuan.entity.center.SliverOrderResult;
import com.zhongxi.guoyuan.url.Url;

import org.json.JSONObject;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class HasConsumeActivity extends BaseActivity implements View.OnClickListener {

    private TextView tvTitle;
    private ImageView ivBack;
    private TextView tvCoin;
    private TextView tvReward;
    private TextView tvBeautiful;
    private ImageView ivDelete;
    private Map<String, String> map = new HashMap<>();
    private TextView tvDoor;
    private TextView tvPost;
    private LinearLayout linearSliver;
    private PullToRefreshListView lvList;
    private String type;
    private String flag = "1";
    private InnerOrderAdapter innerAdapter;
    private List<InnerOrderResult.InnerOrderData> data = new ArrayList<>();
    private List<SliverOrderResult.SliverOrderData> coinData = new ArrayList<>();
    private List<RewardDoorResult.RewardDoorData> doorData = new ArrayList<>();
    private List<RewardDoorResult.RewardDoorData> postData = new ArrayList<>();
    private SliverOrderAdapter coinAdapter;
    private RewardDoorAdapter doorAdapter;
    private RewardDoorAdapter postAdapter;

    @Override
    protected void initView() {
        super.initView();
        setContentView(R.layout.activity_has_consume);
        tvTitle = (TextView) findViewById(R.id.tvTitle);
        tvBeautiful = (TextView) findViewById(R.id.tvBeautiful);
        tvReward = (TextView) findViewById(R.id.tvReward);
        tvCoin = (TextView) findViewById(R.id.tvCoin);
        tvDoor = (TextView) findViewById(R.id.tvDoor);
        tvPost = (TextView) findViewById(R.id.tvPost);
        ivBack = (ImageView) findViewById(R.id.ivBack);
        ivDelete = (ImageView) findViewById(R.id.ivDelete);
        linearSliver = (LinearLayout) findViewById(R.id.linearSliver);
        lvList = (PullToRefreshListView) findViewById(R.id.lvList);
        tvTitle.setText("已消费订单");
        tvDoor.setSelected(true);
    }

    @Override
    protected void initData() {
        super.initData();
        getInner();
    }

    /**
     * 获取美容院订单
     */
    private void getInner() {
        type = "inner";
        getServer(Url.INNER_ORDER, null, "&page="+pageNum, false, null, "get");
    }

    @Override
    protected void processSuccessResult(String response) {
        super.processSuccessResult(response);
        Gson gson = new Gson();
        try{
            if("inner".equals(type)){
                InnerOrderResult result = gson.fromJson(response, InnerOrderResult.class);
                MyApplication.getInstance().setSpValue(result.getSafeobj());
                if(pageNum == 1){
                    data.clear();
                }
                if(null!=result.getData()&&result.getData().size()>0){
                    data.addAll(result.getData());
                }
                lvList.onRefreshComplete();
                innerAdapter = new InnerOrderAdapter(context, data);
                lvList.setAdapter(innerAdapter);
            }else if("sliver".equals(type)){
                SliverOrderResult result = gson.fromJson(response, SliverOrderResult.class);
                MyApplication.getInstance().setSpValue(result.getSafeobj());
                if(pageNum ==1){
                    coinData.clear();
                }
                if(null!=result.getData()&&result.getData().size()>0){
                    coinData.addAll(result.getData());
                }
                lvList.onRefreshComplete();
                coinAdapter = new SliverOrderAdapter(context, coinData);
                lvList.setAdapter(coinAdapter);
            }else if("door".equals(type)){
                RewardDoorResult result = gson.fromJson(response, RewardDoorResult.class);
                MyApplication.getInstance().setSpValue(result.getSafeobj());
                if(pageNum ==1){
                    doorData.clear();
                }
                if(null!=result.getData()&&result.getData().size()>0){
                    doorData.addAll(result.getData());
                }
                String thumbnail = "";
                try{
                    JSONObject obj = new JSONObject(response);
                    thumbnail = obj.getString("thumbnail");
                }catch (Exception e){
                    e.printStackTrace();
                }
                lvList.onRefreshComplete();
                doorAdapter = new RewardDoorAdapter(context, doorData, thumbnail, flag);
                lvList.setAdapter(doorAdapter);
            }else if("post".equals(type)){
                RewardDoorResult result = gson.fromJson(response, RewardDoorResult.class);
                MyApplication.getInstance().setSpValue(result.getSafeobj());
                if(pageNum==1){
                    postData.clear();
                }
                if(null!=result.getData()&&result.getData().size()>0){
                    postData.addAll(result.getData());
                }
                String thumbnail = "";
                try{
                    JSONObject obj = new JSONObject(response);
                    thumbnail = obj.getString("thumbnail");
                }catch (Exception e){
                    e.printStackTrace();
                }
                lvList.onRefreshComplete();
                postAdapter = new RewardDoorAdapter(context, postData, thumbnail, flag);
                lvList.setAdapter(postAdapter);
            }else if("delete".equals(type)){
                BaseResponse result = gson.fromJson(response, BaseResponse.class);
                if(null!=result.getSafeobj()&&!"".equals(result.getSafeobj())){
                    MyApplication.getInstance().setSpValue(result.getSafeobj());
                }
                showShortToast("删除订单成功");
                switch (flag){
                    case "1":
                        getInner();
                        break;
                    case "2":
                        getCoin();
                        break;
                    case "3":
                        getPostReward();
                        break;
                    case "4":
                        getDoorReward();
                        break;
                }
            }
        }catch (Exception e){
            e.printStackTrace();
        }
    }

    @Override
    protected void ProcessFailResult(String response) {
        super.ProcessFailResult(response);
        Gson gson = new Gson();
        try{
            BaseResponse result = gson.fromJson(response, BaseResponse.class);
            if(null!=result.getSafeobj()&&!"".equals(result.getSafeobj())){
                MyApplication.getInstance().setSpValue(result.getSafeobj());
            }
        }catch (Exception e){
            e.printStackTrace();
        }
    }

    @Override
    protected void initEvent() {
        super.initEvent();
        ivBack.setOnClickListener(this);
        tvBeautiful.setOnClickListener(this);
        tvReward.setOnClickListener(this);
        tvCoin.setOnClickListener(this);
        ivDelete.setOnClickListener(this);
        tvDoor.setOnClickListener(this);
        tvPost.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.ivBack:
                finish();
                break;
            case R.id.tvBeautiful:
                //美币美容院消费记录
                pageNum = 1;
                flag = "1";
                linearSliver.setVisibility(View.GONE);
                tvBeautiful.setSelected(true);
                tvCoin.setSelected(false);
                tvReward.setSelected(false);
                getInner();
                break;
            case R.id.tvCoin:
                //银币消费商家消费记录
                pageNum = 1;
                flag = "2";
                linearSliver.setVisibility(View.GONE);
                tvBeautiful.setSelected(false);
                tvCoin.setSelected(true);
                tvReward.setSelected(false);
                getCoin();
                break;
            case R.id.tvReward:
                pageNum = 1;
                flag = "4";
                linearSliver.setVisibility(View.VISIBLE);
                tvBeautiful.setSelected(false);
                tvCoin.setSelected(false);
                tvReward.setSelected(true);
                tvDoor.setSelected(true);
                tvPost.setSelected(false);
                getDoorReward();
                //返银超市上门自提消费记录
                //返银超市邮寄消费记录
                break;
            case R.id.ivDelete:
                //删除订单记录
                deleteOrder();
                break;
            case R.id.tvDoor:
                pageNum = 1;
                flag = "4";
                getDoorReward();
                tvDoor.setSelected(true);
                tvPost.setSelected(false);
                break;
            case R.id.tvPost:
                pageNum = 1;
                flag = "3";
                getPostReward();
                tvDoor.setSelected(false);
                tvPost.setSelected(true);
                break;
        }
    }

    /**
     * 返银上门消费订单
     */
    private void getDoorReward() {
        type = "door";
        getServer(Url.REWARD_TODOOR_ORDER, null, "&page="+pageNum, false, null, "get");
    }

    /**
     * 返银邮寄订单
     */
    private void getPostReward(){
        type = "post";
        getServer(Url.REWARD_POST_ORDER, null, "&page="+pageNum, false, null, "get");
    }

    /**
     * 银币商家订单
     */
    private void getCoin() {
        type = "sliver";
        getServer(Url.SLIVER_ORDER, null, "&page="+pageNum, false, null, "get");
    }

    /**
     * 删除订单记录
     */
    private void deleteOrder() {
        try {
            type = "delete";
            map.clear();
            switch (flag) {
                case "1":
                    map.put("orderids", innerAdapter.getOrderids());
                    break;
                case "2":
                    map.put("orderids", coinAdapter.getOrderids());
                    break;
                case "3":
                    map.put("orderids", postAdapter.getOrderids());
                    break;
                case "4":
                    map.put("orderids", doorAdapter.getOrderids());
                    break;
            }
            map.put("ordertype", flag);
            Log.i("base", "map:" + map.toString());
            getServer(Url.DELETE_ORDER, map, "", false, null, "post");
        }catch (Exception e){
            e.printStackTrace();
        }
    }
}
